package middleware

import (
	"fmt"
	utils "hft/Http/Utils"
	"hft/e"
	"hft/global"
	"hft/tools"

	"github.com/gin-gonic/gin"
)

// 站点检测 看当前站点是否已经改变

func SiteCheck() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		claims, _ := tools.GetClaims(ctx)
		if claims.RoleID == 32 {
			res, err := global.OC_REDIS.Get(fmt.Sprintf("SITE_LOGIN-%v", claims.Belong)).Result()
			if err != nil || res != fmt.Sprintf("%v", claims.BaseClaims.UserID) {
				utils.FailWithMessage(e.NoRightToLoginThisSite, ctx)
				ctx.Abort()
				return
			}
		}
		ctx.Next()
	}
}
